package com.bluefoxyu.game;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Random;

public class GameMap {
    public static final char SNAKE = 'O'; // 蛇的身体
    public static final char FOOD = 'X'; // 苹果
    public static final char EMPTY = ' ';
    public static final char HEAD = 'H'; // 蛇头
    public static final char BORDER = '#'; // 定义边框字符

    private final int width;
    private final int height;
    private final char[][] map;
    private final LinkedList<int[]> snake; // 蛇的身体坐标 [x,y]
    private int[] food;

    public GameMap(int width, int height) {
        this.width = width;
        this.height = height;
        this.map = new char[height][width];
        this.snake = new LinkedList<>();
        init();
    }

    private void init() {
        // 初始蛇头在中间
        int startX = width / 2;
        int startY = height / 2;
        snake.addFirst(new int[]{startX, startY});
        generateFood();
    }

    public void update(Direction direction) {
        int[] head = snake.peekFirst();
        int x = head != null ? head[0] : 0;
        int y = head != null ? head[1] : 0;

        switch (direction) {
            case UP:
                y--;
                break;
            case DOWN:
                y++;
                break;
            case LEFT:
                x--;
                break;
            case RIGHT:
                x++;
                break;
        }

        // 边界判断
        if (x < 0 || y < 0 || x >= width || y >= height) {
            throw new RuntimeException("撞墙了！");
        }

        // 自身碰撞
        for (int[] body : snake) {
            if (body[0] == x && body[1] == y) {
                throw new RuntimeException("撞到自己了！");
            }
        }

        // 添加新头
        snake.addFirst(new int[]{x, y});

        // 吃到食物
        if (food[0] == x && food[1] == y) {
            generateFood();
        } else {
            snake.removeLast(); // 移动尾部
        }
    }

    public void generateFood() {
        Random rand = new Random();
        int x, y;
        do {
            x = rand.nextInt(width);
            y = rand.nextInt(height);
        } while (containsSnake(x, y));
        food = new int[]{x, y};
    }

    private boolean containsSnake(int x, int y) {
        for (int[] part : snake) {
            if (part[0] == x && part[1] == y) return true;
        }
        return false;
    }

    public void render(String sessionId) {
        // 初始化空地图
        for (char[] row : map) {
            Arrays.fill(row, EMPTY);
        }

        // 绘制食物
        map[food[1]][food[0]] = FOOD;

        // 绘制蛇
        for (int i = 0; i < snake.size(); i++) {
            int[] part = snake.get(i);
            map[part[1]][part[0]] = i == 0 ? HEAD : SNAKE; // H 表示头部
        }

        // 打印地图
        System.out.println("\n=== 用户（"+sessionId+"）的 Game Map ===");
        // 打印上边框
        for (int i = 0; i < width + 2; i++) {
            System.out.print(BORDER + " ");
        }
        System.out.println();

        for (char[] row : map) {
            System.out.print(BORDER + " "); // 打印左边框
            for (char c : row) {
                System.out.print(c + " ");
            }
            System.out.println(BORDER); // 打印右边框
        }

        // 打印下边框
        for (int i = 0; i < width + 2; i++) {
            System.out.print(BORDER + " ");
        }
        System.out.println();
    }
}

